﻿/*
 * @Autor: PK
 * @Date: 2021/12/16 16:44:SS
 */

using System;
using System.IO;
using System.Windows.Forms;

namespace ShkSimulation.desktop.util {
	public class FileUtil {

		public static bool CreateDirectory(string path) {
			if (Directory.Exists(path)) {
				return true;
			}
			try {
				Directory.CreateDirectory(path);
				return true;
			} catch (Exception e) {
				Console.WriteLine(e);
				return false;
			}
		}

		public static string OpenFile(OpenFileDialog openFileDialog, string title = "打开文件", string filter = "所有文件|*.*") {
			string originTitle = openFileDialog.Title;
			string originFilter = openFileDialog.Filter;
			openFileDialog.Filter = filter;
			openFileDialog.Title = title;
			DialogResult openResult = openFileDialog.ShowDialog();
			openFileDialog.Filter = originFilter;
			openFileDialog.Title = originTitle;
			if (openResult == DialogResult.OK) {
				return openFileDialog.FileName;
			}
			return null;
		}
		
		public static string OpenFolder(FolderBrowserDialog folder, string description) {
			folder.UseDescriptionForTitle = true;
			folder.Description = description;
			DialogResult openResult = folder.ShowDialog();
			if (openResult == DialogResult.OK) {
				return folder.SelectedPath;
			}
			return null;
		}

		public static string SaveFile(SaveFileDialog saveFileDialog, string title, string filter, string fileExt) {
			saveFileDialog.Title = title;
			saveFileDialog.Filter = filter;
			saveFileDialog.FileName = fileExt;
			DialogResult openResult = saveFileDialog.ShowDialog();
			if (openResult == DialogResult.OK) {
				return saveFileDialog.FileName;
			}
			return null;
		}

	}
}